{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "37e262a1b60a0848",
   "metadata": {},
   "source": [
    "# Python格式化打印教程\n",
    "\n",
    "## 1. 基础字符串格式化\n",
    "\n",
    "### 1.1 使用f-string和对齐控制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "facdfd86b43bffd0",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2025-03-15T07:42:17.234128Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Python              3.14                \n"
     ]
    }
   ],
   "source": [
    "# 基本语法：{变量:<宽度}\n",
    "name = \"Python\"\n",
    "value = 3.14\n",
    "print(f'  {name:<20}{value:<20}')  # 左对齐，各占20个字符宽度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2200a50-388e-4fd8-aaef-0254f2269b80",
   "metadata": {},
   "source": [
    "### 1.2 多列对齐打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aa6d7e48-e083-43e9-84a6-a99edc78d361",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Name:               Python              Version:            3.9                 \n"
     ]
    }
   ],
   "source": [
    "# 同时打印多列数据\n",
    "name = \"Python\"\n",
    "version = 3.9\n",
    "print(f'  {\"Name:\":<20}{name:<20}{\"Version:\":<20}{version:<20}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c210de2-f5e4-466f-93e8-9a38345d964c",
   "metadata": {},
   "source": [
    "## 2. ANSI转义序列添加颜色和样式\n",
    "\n",
    "### 2.1 基本样式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d717a766-d0ff-4e47-81eb-28b28af5e87b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001B[1m标题\u001B[0m\n"
     ]
    }
   ],
   "source": [
    "# 粗体文本\n",
    "print(\"\\033[1m\" + \"标题\" + \"\\033[0m\")\n",
    "\n",
    "# 常用ANSI代码：\n",
    "# \\033[0m  - 重置所有样式\n",
    "# \\033[1m  - 粗体\n",
    "# \\033[4m  - 下划线\n",
    "# \\033[31m - 红色文本\n",
    "# \\033[32m - 绿色文本\n",
    "# \\033[33m - 黄色文本"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc8e4600-0bf1-425f-8f68-275f615fd8b1",
   "metadata": {},
   "source": [
    "## 3. 分组信息打印\n",
    "\n",
    "### 3.1 使用分节标题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e2a2419c-8faf-4168-993f-d5a74b0720e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_info(data):\n",
    "    # 打印标题和分组\n",
    "    print(\"\\033[1m\" + \"基本信息\" + \"\\033[0m\")\n",
    "    print(f'  {\"名称:\":<20}{data.name:<20}')\n",
    "    print(f'  {\"版本:\":<20}{data.version:<20}')\n",
    "    print()  # 空行分隔\n",
    "\n",
    "    print(\"\\033[1m\" + \"详细信息\" + \"\\033[0m\")\n",
    "    print(f'  {\"作者:\":<20}{data.author:<20}')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "136d1d67-c003-4263-a6aa-fc14948b5731",
   "metadata": {},
   "source": [
    "### 3.2 条件性打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d036f50f-d398-4fd3-af2a-1a256325efec",
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_details(data):\n",
    "    if data.type == \"package\":\n",
    "        print(\"\\033[1m\" + \"包信息\" + \"\\033[0m\")\n",
    "        print(f'  {\"依赖数量:\":<20}{len(data.dependencies):<20}')\n",
    "\n",
    "    if data.type == \"module\":\n",
    "        print(\"\\033[1m\" + \"模块信息\" + \"\\033[0m\")\n",
    "        print(f'  {\"函数数量:\":<20}{data.function_count:<20}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f75a8c1c-32a8-49af-9c87-10a7075185e2",
   "metadata": {},
   "source": [
    "## 4. 复杂数据结构打印\n",
    "\n",
    "### 4.1 列表转字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d4f98907-49c9-45cc-a50a-19058507f147",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  数字列表:               1, 2, 3, 4, 5       \n"
     ]
    }
   ],
   "source": [
    "# 将列表转换为逗号分隔的字符串\n",
    "numbers = [1, 2, 3, 4, 5]\n",
    "numbers_str = ', '.join(map(str, numbers))\n",
    "print(f'  {\"数字列表:\":<20}{numbers_str:<20}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3294bd9a-0bb9-4601-ae61-ff856c981e04",
   "metadata": {},
   "source": [
    "## 5. 实用技巧\n",
    "\n",
    "### 5.1 创建可重用的打印函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "319c58a9-80ae-47b2-876b-5e8ac96ec2ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001B[1m配置信息\u001B[0m\n",
      "  名称                  测试项目                \n",
      "  版本                  1.0.0               \n",
      "\n"
     ]
    }
   ],
   "source": [
    "def print_section(title, data_dict):\n",
    "    print(\"\\033[1m\" + title + \"\\033[0m\")\n",
    "    for key, value in data_dict.items():\n",
    "        print(f'  {key:<20}{value:<20}')\n",
    "    print()\n",
    "\n",
    "\n",
    "# 使用示例\n",
    "config = {\n",
    "    \"名称\": \"测试项目\",\n",
    "    \"版本\": \"1.0.0\"\n",
    "}\n",
    "print_section(\"配置信息\", config)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86931cd7-1305-4f9e-9c87-f669ba6cc9d7",
   "metadata": {},
   "source": [
    "### 5.2 使用常量提高可维护性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fd9cd558-098f-43c0-a7dd-27384fce9b80",
   "metadata": {},
   "outputs": [],
   "source": [
    "SECTION_WIDTH = 20\n",
    "INDENT = \"  \"\n",
    "BOLD = \"\\033[1m\"\n",
    "RESET = \"\\033[0m\"\n",
    "\n",
    "\n",
    "def print_item(label, value):\n",
    "    print(f'{INDENT}{label:<{SECTION_WIDTH}}{value:<{SECTION_WIDTH}}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
